package system

import (
	"context"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
	"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
	"log"
	"new_club_recruitment/global"
)

type WechatPayServe struct{}

var (
	ctx                        = context.Background()
	mchID                      = global.GVA_CONFIG.WeChatPay.MchID                      // 商户号
	mchCertificateSerialNumber = global.GVA_CONFIG.WeChatPay.MchCertificateSerialNumber // 商户证书序列号
	mchAPIv3Key                = global.GVA_CONFIG.WeChatPay.MchAPIv3Key                // 商户APIv3密钥
)

func (WechatPay *WechatPayServe) Client() *core.Client {

	// 使用 utils 提供的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
	mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
	if err != nil {
		log.Print("加载商户私钥错误")
	}
	// 使用商户私钥等初始化 client，并使它具有自动定时获取微信支付平台证书的能力
	opts := []core.ClientOption{option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key)}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		log.Printf("新的微信支付客户端错误:%s", err)
	}
	return client
}

// CloseOrder 关闭订单
func (WechatPay *WechatPayServe) CloseOrder(OutTradeNo, Mchid *string) (code int, err error) {
	svc := jsapi.JsapiApiService{Client: WechatPay.Client()}
	result, err := svc.CloseOrder(ctx,
		jsapi.CloseOrderRequest{
			OutTradeNo: OutTradeNo, //出单号
			Mchid:      Mchid,      //商户号
		},
	)
	return result.Response.StatusCode, err
}

// Prepay 预付款
func (WechatPay *WechatPayServe) Prepay(PrepayRequest jsapi.PrepayRequest) (*jsapi.PrepayResponse, int, error) {
	svc := jsapi.JsapiApiService{Client: WechatPay.Client()}
	resp, result, err := svc.Prepay(ctx, PrepayRequest)
	return resp, result.Response.StatusCode, err
}

// QueryOrderById 通过id查找订单
func (WechatPay *WechatPayServe) QueryOrderById(TransactionId, Mchid *string) (*payments.Transaction, int, error) {
	svc := jsapi.JsapiApiService{Client: WechatPay.Client()}
	resp, result, err := svc.QueryOrderById(ctx,
		jsapi.QueryOrderByIdRequest{
			TransactionId: TransactionId, //交易id
			Mchid:         Mchid,         //商户id
		},
	)
	return resp, result.Response.StatusCode, err
}

// QueryOrderByOutTradeNo 出单号查询订单
func (WechatPay *WechatPayServe) QueryOrderByOutTradeNo(OutTradeNo, Mchid *string) (*payments.Transaction, int, error) {
	svc := jsapi.JsapiApiService{Client: WechatPay.Client()}
	resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
		jsapi.QueryOrderByOutTradeNoRequest{
			OutTradeNo: OutTradeNo, //出单号
			Mchid:      Mchid,
		},
	)
	return resp, result.Response.StatusCode, err
}

// Create 创建退款
func (WechatPay *WechatPayServe) Create(CreateRequest refunddomestic.CreateRequest) (*refunddomestic.Refund, int, error) {
	svc := refunddomestic.RefundsApiService{Client: WechatPay.Client()}
	resp, result, err := svc.Create(ctx, CreateRequest)
	return resp, result.Response.StatusCode, err
}

// QueryByOutRefundNo 查询退票否
func (WechatPay *WechatPayServe) QueryByOutRefundNo(OutRefundNo, SubMchid *string) (*refunddomestic.Refund, int, error) {
	svc := refunddomestic.RefundsApiService{Client: WechatPay.Client()}
	resp, result, err := svc.QueryByOutRefundNo(ctx,
		refunddomestic.QueryByOutRefundNoRequest{
			OutRefundNo: OutRefundNo,
			SubMchid:    SubMchid,
		},
	)
	return resp, result.Response.StatusCode, err
}
